<template>
  <div>
    <kendoForm
      :kendo-form-items="kendoFormItems"
      :kendo-form="kendoForm"
      :kendo-params-value="kendoParamsValue"
    />
  </div>
</template>

<script>
  import kendoForm from '@/Views/components/kendo-vue-component/kendoForm/index.vue'
  import { isEmpty } from '@/Utils/strUtil.js'
  export default {
    name: 'PrjOperateCmpt',
    components: {
      kendoForm
    },
    data() {
      return {
        dialogName: this.$attrs.data.dialogName,
        kendoFormItems: [
          {
            ref: 'PRJ_CD',
            remoteData: true,
            cascade: true,
            name: 'relPrjNo',
            label: '关联项目名称',
            paramName: 'relPrjNo',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/prjTransDict',
            autoBind: true,
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'PRJ_CD' })
              }
            }
          },
          {
            id: 'memberNo',
            name: 'memberNo',
            label: '项目成员',
            paramName: 'memberNo',
            required: true,
            viewDisabled: true,
            controlType: 'multicolumncombobox',
            dataSource: 'PrjMemberDataSource',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/prjMemberMng/findPrjMemberList',
            dataMapping: {
              label: 'memberName',
              value: 'memberNo'
            },
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            },
            column: [
              {
                field: 'relPrjNo',
                title: '所属项目',
                width: 180
              },
              {
                field: 'memberNo',
                title: '项目成员编号',
                width: 120
              },
              {
                field: 'memberName',
                title: '项目成员名称',
                width: 120
              }
            ]
          },
          {
            name: 'performReqNo',
            label: '当前执行需求',
            paramName: 'performReqNo',
            required: true,
            controlType: 'multiselect',
            autoBind: false,
            remoteData: true,
            dataSource: 'PrjReqTaskDataSource',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/reqmntTaskMng/findReqMntTaskList',
            dataMapping: {
              label: 'reqName',
              value: 'reqNo'
            },
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            }
          },
          {
            name: 'extraWorkDt',
            label: '加班日期',
            paramName: 'extraWorkDt',
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            required: true,
            change: this.extraWorkDtChange
          },
          {
            id: 'extraWorkStartTime',
            name: 'extraWorkStartTime',
            label: '加班开始时间',
            paramName: 'extraWorkStartTime',
            controlType: 'dateTimePicker',
            format: 'yyyy-MM-dd HH:mm:ss',
            timeFormat: 'HH:mm:ss',
            interval: 5,
            change: this.startChange,
            required: true
          },
          {
            id: 'extraWorkEndTime',
            name: 'extraWorkEndTime',
            label: '加班结束时间',
            paramName: 'extraWorkEndTime',
            controlType: 'dateTimePicker',
            format: 'yyyy-MM-dd HH:mm:ss',
            timeFormat: 'HH:mm:ss',
            interval: 5,
            change: this.endChange,
            required: true
          },
          {
            name: 'extraWorkDuration',
            label: '加班时长(小时)',
            paramName: 'extraWorkDuration',
            controlType: 'numeric',
            disabled: true,
            viewDisabled: true,
            required: true
          },
          {
            name: 'extraWorkDays',
            label: '加班天数',
            paramName: 'extraWorkDays',
            controlType: 'numeric',
            disabled: true,
            viewDisabled: true,
            required: true
          },
          {
            ref: 'DICT_STATUS_CD',
            remoteData: true,
            cascade: true,
            name: 'whetherConfirmValidity',
            label: '是否确认有效',
            paramName: 'whetherConfirmValidity',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            autoBind: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'DICT_STATUS_CD' })
              }
            }
          },
          {
            genre: 'special',
            name: 'extraWorkReas',
            label: '加班原因',
            paramName: 'extraWorkReas',
            required: true,
            controlType: 'textarea'
          },
          {
            ref: 'extraWorkReplyInfo',
            genre: 'special',
            name: 'extraWorkReplyInfo',
            label: '加班批复信息补充',
            paramName: 'extraWorkReplyInfo',
            required: false,
            editDisabled: true,
            controlType: 'editor',
            imageBrowser: {
              transport: {
                read: {
                  url: '/service/imageBrowser/read',
                  type: 'POST'
                },
                destroy: {
                  url: '/service/imageBrowser/destroy',
                  type: 'POST'
                },
                create: {
                  url: '/service/imageBrowser/create',
                  type: 'POST'
                },
                thumbnailUrl: '/service/imagePreview/thumbnail',
                uploadUrl: '/service/imageBrowser/upload',
                imageUrl: '/service/imagePreview/image?path={0}'
              },
              schema: {
                data: function(response) {
                  return response.data
                },
                model: {
                  id: 'id',
                  fields: {
                    name: 'materialName',
                    type: 'materialType',
                    size: 'materialSize'
                  }
                }
              }
            },
            tools: this.editorTools
          }
        ],
        kendoForm: {
          ref: 'prodForm',
          valid: 'valid',
          buttons: [
            {
              name: '保存', // 按钮内容
              class: 'k-primary', // 按钮样式
              icon: 'save',
              click: this.save
            },
            {
              name: '重置', // 按钮内容
              icon: 'reset-sm',
              click: this.reset
            }
          ]
        },
        kendoParamsValue: {
          id: '',
          relPrjNo: '',
          memberNo: '',
          performReqNo: '',
          extraWorkDt: '',
          extraWorkStartTime: '',
          extraWorkEndTime: '',
          extraWorkDuration: '',
          extraWorkDays: '',
          whetherConfirmValidity: '',
          extraWorkReas: '',
          extraWorkReplyInfo: ''
        }
      }
    },
    mounted() {
      const operatorType = this.$attrs.data.operatorType
      if (operatorType === 'view') {
        this.kendoParamsValue = Object.assign(this.kendoParamsValue, this.deepCopy(this.$attrs.data.param))
        this.kendoFormItems.forEach(element => {
          if (!isEmpty(this.kendoParamsValue[element.paramName]) &&
            typeof this.kendoParamsValue[element.paramName] === 'string' &&
            this.kendoParamsValue[element.paramName].split(',').length > 1) {
            this.$set(this.kendoParamsValue, element.paramName, this.kendoParamsValue[element.paramName].split(','))
          }
          if (element.paramName === 'extraWorkStartTime' || element.paramName === 'extraWorkEndTime') {
            debugger
            // eslint-disable-next-line no-undef
            this.$set(this.kendoParamsValue, element.paramName, kendo.parseDate(this.kendoParamsValue[element.paramName], element.format, 'zh-CN'))
          }
          if (element.viewDisabled) {
            element.disabled = true
          }
        })
      } else if (operatorType === 'edit') {
        this.kendoFormItems.forEach(item => {
          // Array.isArray(this.kendoParamsValue[item.paramName])
          if (!isEmpty(this.kendoParamsValue[item.paramName]) &&
            typeof this.kendoParamsValue[item.paramName] === 'string' &&
            this.kendoParamsValue[item.paramName].split(',').length > 1) {
            this.$set(this.kendoParamsValue, item.paramName, this.kendoParamsValue[item.paramName].split(','))
          }
          if (item.paramName === 'extraWorkStartTime' || item.paramName === 'extraWorkEndTime') {
            debugger
            // eslint-disable-next-line no-undef
            this.$set(this.kendoParamsValue, item.paramName, kendo.parseDate(this.kendoParamsValue[item.paramName], item.format, 'zh-CN'))
          }
          if (item.editDisabled) {
            item['readonly'] = true
          }
        })
      }
    },
    methods: {
      extraWorkDtChange(e) {
        const { extraWorkDt, extraWorkStartTime, extraWorkEndTime } = this.kendoParamsValue
        if (extraWorkStartTime && extraWorkEndTime && extraWorkDt) {
          const parameterMap = {}
          parameterMap.extraWorkDt = this.dateFormat(e.sender.value(), 'yyyy-MM-dd')
          parameterMap.startDate = extraWorkStartTime
          parameterMap.endDate = extraWorkEndTime
          // 根据当前加班时间段 确定加班时长。
          this.$post('/service/prjMng/prjMemberMng/calculateExtraWorkDuration', parameterMap).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.kendoParamsValue = Object.assign(this.kendoParamsValue, res.data)
            }
          })
        } else {
          this.$KendoNotify({
            title: '提示信息:',
            message: '加班日期、加班开始时间、加班结束时间 必须填写完整'
          }, 'warning')
        }
      },
      startChange: function(e) {
        const { extraWorkDt } = this.kendoParamsValue
        // eslint-disable-next-line no-undef
        var start = kendo.jQuery('#extraWorkStartTime').data('kendoDateTimePicker')
        // eslint-disable-next-line no-undef
        var end = kendo.jQuery('#extraWorkEndTime').data('kendoDateTimePicker')
        var startDate = e.sender.value()
        var endDate = end.value()
        if (startDate && endDate && extraWorkDt) {
          const parameterMap = {}
          parameterMap.extraWorkDt = extraWorkDt
          parameterMap.startDate = this.dateFormat(startDate, 'yyyy-MM-dd HH:mm:ss')
          parameterMap.endDate = this.dateFormat(endDate, 'yyyy-MM-dd HH:mm:ss')
          // 根据当前加班时间段 确定加班时长。
          this.$post('/service/prjMng/prjMemberMng/calculateExtraWorkDuration', parameterMap).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.kendoParamsValue = Object.assign(this.kendoParamsValue, res.data)
            }
          })
        } else {
          this.$KendoNotify({
            title: '提示信息:',
            message: '加班日期、加班开始时间、加班结束时间 必须填写完整'
          }, 'warning')
        }
        if (startDate) {
          startDate = new Date(startDate)
          startDate.setDate(startDate.getDate())
          end.min(startDate)
        } else if (endDate) {
          start.max(new Date(endDate))
        } else {
          endDate = new Date()
          start.max(endDate)
          end.min(endDate)
        }
      },
      endChange: function(e) {
        const { extraWorkDt } = this.kendoParamsValue
        // eslint-disable-next-line no-undef
        var start = kendo.jQuery('#extraWorkStartTime').data('kendoDateTimePicker')
        // eslint-disable-next-line no-undef
        var end = kendo.jQuery('#extraWorkEndTime').data('kendoDateTimePicker')
        var endDate = e.sender.value()
        var startDate = start.value()
        if (startDate && endDate && extraWorkDt) {
          const parameterMap = {}
          parameterMap.extraWorkDt = extraWorkDt
          parameterMap.startDate = this.dateFormat(startDate, 'yyyy-MM-dd HH:mm:ss')
          parameterMap.endDate = this.dateFormat(endDate, 'yyyy-MM-dd HH:mm:ss')
          // 根据当前加班时间段 确定加班时长。
          this.$post('/service/prjMng/prjMemberMng/calculateExtraWorkDuration', parameterMap).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.kendoParamsValue = Object.assign(this.kendoParamsValue, res.data)
            }
          })
        } else {
          this.$KendoNotify({
            title: '提示信息:',
            message: '加班日期、加班开始时间、加班结束时间 必须填写完整'
          }, 'warning')
        }
        if (endDate) {
          endDate = new Date(endDate)
          endDate.setDate(endDate.getDate())
          start.max(endDate)
        } else if (startDate) {
          end.min(new Date(startDate))
        } else {
          endDate = new Date()
          start.max(endDate)
          end.min(endDate)
        }
      },
      save(paramVal, validator) {
        this.kendoFormItems.forEach(item => {
          if (Array.isArray(paramVal[item.paramName])) {
            this.$set(paramVal, item.paramName, paramVal[item.paramName].join(','))
          }
          if (item.paramName === 'extraWorkStartTime' || item.paramName === 'extraWorkEndTime') {
            this.$set(paramVal, item.paramName, this.dateFormat(paramVal[item.paramName], item.format))
          }
        })
        if (validator.validate()) {
          this.$post('/service/prjMng/prjMemberMng/saveOrUpdatePrjMemberExtraWork', paramVal).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.$KendoNotify({
                title: '提示信息:',
                message: res.message
              }, 'success')
              this.$bus.emit('dialog' + `${this.dialogName}`, true)
            }
          })
        }
      },
      reset(paramVal) {
        Object.keys(paramVal).forEach(key => {
          if (Array.isArray(paramVal[key])) {
            this.kendoParamsValue[key] = []
          } else {
            this.kendoParamsValue[key] = ''
          }
        })
      }
    }
  }
</script>

<style>

</style>
